﻿// ModuleTreeView.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "ModuleTreeView.h"

#include "ModuleLibraryDialog.h"
#include "OperatorDialog.h"

#define		MOD_TREE_ROOT_ICON	0
#define		MOD_TREE_MODU_ICON	1
#define		MOD_TREE_OPER_ICON	2

// CModuleTreeView

IMPLEMENT_DYNCREATE(CModuleTreeView, CTreeView)

CModuleTreeView::CModuleTreeView()
{

}

CModuleTreeView::~CModuleTreeView()
{
}

BEGIN_MESSAGE_MAP(CModuleTreeView, CTreeView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(NM_CLICK, &CModuleTreeView::OnNMClick)
	ON_NOTIFY_REFLECT(NM_DBLCLK, &CModuleTreeView::OnNMDblclk)
	ON_NOTIFY_REFLECT(NM_RCLICK, &CModuleTreeView::OnNMRClick)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, &CModuleTreeView::OnTvnItemExpanding)
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, &CModuleTreeView::OnTvnItemExpanded)
END_MESSAGE_MAP()


// CModuleTreeView 诊断

#ifdef _DEBUG
void CModuleTreeView::AssertValid() const
{
	CTreeView::AssertValid();
}

#ifndef _WIN32_WCE
void CModuleTreeView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif
#endif //_DEBUG


// CModuleTreeView 消息处理程序


BOOL CModuleTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此添加专用代码和/或调用基类
	cs.style |= TVS_HASLINES | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | TVS_NOHSCROLL | TVS_EX_FADEINOUTEXPANDOS;

	return CTreeView::PreCreateWindow(cs);
}


int CModuleTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CTreeCtrl& tree = GetTreeCtrl();

	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_MODULE_TREE_24))
	{
		m_il.Create(24, 24, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		tree.SetImageList(&m_il, TVSIL_NORMAL);
	}

	//InitTreeCtrl();

	return 0;
}

void CModuleTreeView::Populate()
{
	CTreeCtrl& tree = GetTreeCtrl();

	tree.DeleteAllItems();
	tree.InsertItem("模块", MOD_TREE_ROOT_ICON, MOD_TREE_ROOT_ICON, TVI_ROOT);

	HTREEITEM hRoot = tree.GetRootItem();

	ck::Module* pModule = NULL;
	ck::ModuleManager* pModManager = ck::ModuleManager::GetInstance();

	int nCount = pModManager->GetCount();
	for (int i = 0; i < nCount; i++)
	{
		pModule = pModManager->GetModule(i);
		
		//HTREEITEM hItem = tree.InsertItem(pModule->GetID(), MOD_TREE_MODU_ICON, MOD_TREE_MODU_ICON, hRoot);
		HTREEITEM hItem = tree.InsertItem(pModule->GetName(), MOD_TREE_MODU_ICON, MOD_TREE_MODU_ICON, hRoot);
		tree.SetItemData(hItem, (DWORD_PTR)pModule);
	}

	tree.Expand(hRoot, TVE_EXPAND);
	

}


/************************************************************************/
/* 左键单击事件                                                          */
/************************************************************************/
void CModuleTreeView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}

	tree.Select(hItem, TVGN_CARET);

	if (hItem == tree.GetRootItem())
	{
		//根节点
		CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
		pSplitterWnd->PopulateList();
	}
	else if (tree.GetParentItem(hItem) == tree.GetRootItem())
	{
		//Module节点
		ck::Module* pModule = (ck::Module*)tree.GetItemData(hItem);
		if (pModule)
		{
			CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
			pSplitterWnd->PopulateList(pModule);
		}
	}
	else
	{
		//Operator节点
		ck::Operator* pOperator = (ck::Operator*)tree.GetItemData(hItem);
		if (pOperator)
		{
			CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
			pSplitterWnd->PopulateList(pOperator);
		}
	}

	////更新ListView
	//CModuleSplitterWnd* pWnd = (CModuleSplitterWnd*)(this->GetParent());
	//unsigned int ckCode = 1;
	////pWnd->PopulateList(1, tree.GetItemText(hItem));
}

/************************************************************************/
/* 左键双击事件                                                          */
/************************************************************************/
void CModuleTreeView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}
	tree.Select(hItem, TVGN_CARET);

	if (hItem== tree.GetRootItem())
	{
		//根节点
		CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
		pSplitterWnd->PopulateList();
	}
	else if (tree.GetParentItem(hItem) == tree.GetRootItem())
	{
		//Module节点
		ck::Module* pModule = (ck::Module*)tree.GetItemData(hItem);
		if (pModule)
		{
			PopulateModule(pModule, hItem);
			CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
			pSplitterWnd->PopulateList(pModule);
		}
	}
	else
	{
		//Operator节点
		ck::Operator* pOperator = (ck::Operator*)tree.GetItemData(hItem);
		if (pOperator)
		{
			CModuleSplitterWnd* pSplitterWnd = (CModuleSplitterWnd*)GetParent();
			pSplitterWnd->PopulateList(pOperator);

			COperatorDialog dlg;
			dlg.SetOperator(pOperator);
			dlg.DoModal();
		}
	}

	GetTreeCtrl().Expand(hItem, TVE_EXPAND);


	*pResult = 0;
}

/************************************************************************/
/* 右键单击事件                                                          */
/************************************************************************/
void CModuleTreeView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	CTreeCtrl& tree = GetTreeCtrl();

	CPoint pt;
	if (!GetCursorPos(&pt))
	{
		return;
	}

	tree.ScreenToClient(&pt);
	UINT uFlag = 0;
	HTREEITEM hItem = tree.HitTest(pt, &uFlag);
	if ((NULL == hItem) || !(TVHT_ONITEM&uFlag))
	{
		return;
	}


	tree.Select(hItem, TVGN_CARET);
	CString strText = tree.GetItemText(hItem);
	AfxMessageBox(strText);

	*pResult = 0;
}

void CModuleTreeView::OnTvnItemExpanding(NMHDR *pNMHDR, LRESULT *pResult)
{
	//AfxMessageBox("Expanding");
	*pResult = 0;
}

void CModuleTreeView::OnTvnItemExpanded(NMHDR *pNMHDR, LRESULT *pResult)
{
	//AfxMessageBox("Expanded");
	*pResult = 0;
}



void CModuleTreeView::PopulateModule(ck::Module* pModule, HTREEITEM hParent)
{
	int nCount = 0;
	ck::Operator* pOperator = NULL;

	CTreeCtrl& tree = GetTreeCtrl();
	DeleteAllChildItems(hParent);

	nCount = pModule->GetOperatorCount();
	for (int i = 0; i < nCount; i++)
	{
		pOperator = pModule->GetOperator(i);
		
		HTREEITEM hItem = tree.InsertItem(pOperator->GetName(), MOD_TREE_OPER_ICON, MOD_TREE_OPER_ICON, hParent);
		tree.SetItemData(hItem, (DWORD_PTR)pOperator);

	}

	tree.Expand(hParent, TVE_EXPAND);
}

void CModuleTreeView::DeleteAllChildItems(HTREEITEM hParent)
{
	CTreeCtrl& tree = GetTreeCtrl();

	HTREEITEM hNext = NULL;
	HTREEITEM hItem = tree.GetChildItem(hParent);
	while ((hItem = tree.GetChildItem(hParent)) != NULL)
	{
		tree.DeleteItem(hItem);
	}
}




